<%@page import="java.text.SimpleDateFormat"%>
<%@page import="weaver.conn.RecordSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
         import="com.zlgs.cqyf.vote.ConnectionUtil" %>
<%@ page import="com.alibaba.fastjson.*" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.util.Date" %>
<%
    final long ONE_MILLISECOND = 1000L;
    final long ONE_MINUTE = 60000L;
    final long ONE_HOUR = 3600000L;
    final long ONE_DAY = 86400000L;

    final String ONE_SECOND_AGO = "秒前";
    final String ONE_MINUTE_AGO = "分钟前";
    final String ONE_HOUR_AGO = "小时前";
    final String ONE_DAY_AGO = "天前";

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    JSONObject retObject = new JSONObject();
    retObject.put("code", 200);
    retObject.put("msg", "操作成功");
    try {
        // URL 参数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer awards_participator_id = Integer.parseInt(request.getParameter("awardsParticipatorId"));
        Date date = new Date();
        if (awards_participator_id == null) {
            retObject.put("code", -1);
            retObject.put("msg", "awardsParticipatorId 不能为空");
        } else {
            JSONArray array = new JSONArray();
            // 数据库查询
            String sql = "select * from vote_awards_message where awards_participator_id=" + awards_participator_id.toString() + " order by create_time desc";
            RecordSet set = new RecordSet();
            set.executeSql(sql);
            while (set.next()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", set.getString("id"));                       // 留言id
                jsonObject.put("name", set.getString("name"));                 // 留言人姓名
                jsonObject.put("photo", set.getString("photo"));               // 留言人头像
                jsonObject.put("dept", set.getString("dept"));                 // 留言人部门名称
                jsonObject.put("message", set.getString("message"));           // 留言
                jsonObject.put("createTime", set.getDate("create_time"));           // 留言
                
                String createTimeStr  = set.getString("create_time");
     	       if(null!=createTimeStr && !"".equals(createTimeStr)){
     	    	   try{
     	    		  createTimeStr =   sdf.format(set.getDate("create_time"));
     	    	   }catch(Exception e){}
     	       }
                jsonObject.put("createTimeStr", createTimeStr);           // 留言时间
                if (null != set.getDate("create_time")) {
                    long now = date.getTime();
                    long delta = now - set.getDate("create_time").getTime();
                    long time2 = set.getDate("create_time").getTime() - 1000 * 60 * 60 * 24;

                    // 逻辑判断
                    String str = "";
                    if (delta < ONE_MINUTE) {
                        str = delta / ONE_MILLISECOND + ONE_SECOND_AGO;
                    } else if (delta < ONE_HOUR) {
                        str = delta / ONE_MINUTE + ONE_MINUTE_AGO;
                    } else if (delta < ONE_DAY) {
                        str = delta / ONE_HOUR + ONE_HOUR_AGO;
                    } else if (delta < time2) {
                        str = delta / ONE_DAY + ONE_DAY_AGO;
                    }
                    jsonObject.put("timer", str);                                // 时间间隔
                }
                array.add(jsonObject);
            }
            retObject.put("data", array);
        }
    } catch (Exception e) {
        e.printStackTrace();
        retObject.put("code", -1);
        retObject.put("msg", e.getMessage());
    } finally {
        PrintWriter printWriter = response.getWriter();
        printWriter.append(retObject.toJSONString());
        if (printWriter != null) printWriter.close();
    }
%>
